<?php
declare (strict_types = 1);

namespace app\home\controller;

use app\api\model\Category;
use app\api\model\Spec_goods;
use app\api\model\Spec_value;
use app\BaseController;
use think\Request;

class Goods extends Base
{
    public function index($id){
        // 获取商品信息
        $where=[];

        if ($id){
            $where['cate_id']=$id;
        }
        // 根据商品分类id查询商品
        $list=\app\api\model\Goods::getGoodsList($id,$where);

        // 根据id查询分类信息
        $cateInfo=Category::findCategory($id);
        return view('list',[
            'goodsList'=>$list,
            'cateInfo'=>$cateInfo
        ]);
    }

    public function goodsItem($id){
       $goods=\app\api\model\Goods::with(['images','specGoods'])->find($id)->toArray();
       $goods['goods_attr']=json_decode($goods['goods_attr']);
       $spec_goods_values=Spec_goods::getSpecGoods($id);
        $spec_goods=json_encode($spec_goods_values);
        return view('item',[
            'info'=>$goods,
            'spec_goods_value'=> $spec_goods_values,
            'spec_goods'=>$spec_goods,
        ]);
    }

    public function store(Request $request){
        $id=$request->post('id');
        $data=Spec_goods::getspecGoodsNum($id);
        $store_count=$data['store_count'];
        return json(['store'=>$store_count]);
    }
}
